<?php

namespace Admin\Controller;
use Think\Controller;
use Think\Page;
//会员表
class UserController extends CommonController{
	//会员列表
	public function index()
	{
		$user=M('user_list')->where(array('ustate'=>1))->select();
		$this->assign("list",$user);
		$this->display();
	}
    //删除（改变用户状态，不删除）
    public function del()
    {
        $id=I("get.id");
        $data['ustate']=0;
        $result=M("user_list")->where(array("user_id"=>$id))->save($data);
        if($result){
            $this->success("删除成功！",U('index'),1);
        }else{
            $this->error("删除失败！",U(""),1);
        }
    }
    

    //编辑
    public function edit()
    {
        $user_id =intval(I("get.id"));
        //编辑显示数据
        if ($user_id){
            //获取用户名信息
            $this->info = M('user_list')->where(array("user_id"=>$user_id))->find();
            $this->display();
        }
        //提交处理模块
        if (IS_POST){
            $data=I("post.");
            $res=M('user_list')->save($data);
            if($res){
                $this->success("更新成功!","index",1);
            }else{
                $this->error("更新失败","",1);
            }
        }

    }	

    /*
   *
   * 用户成长值列表
   * */
    public function growth_user(){
        $type_id=I("post.type_id");
        $key_world=I("post.goods_name");

        $obj=M("growth_user");
        $num=10;
        if($type_id==3){
            $user_id='';
            $user_ids=M("user_list")->field("user_id")->where("name like '%{$key_world}%'")->select();
            foreach($user_ids as $idk=>$idv){
                $user_id.=$idv['user_id'].',';
            }
            $user_id=trim("$user_id",",");
            if($user_id==''){
                $this->redirect('growth_user','',3,'没有匹配的记录！');
            }else{
                $count=$obj->where("del=0 and user_id in($user_id)")->count();
                $page=new Page($count,$num);
                $arr=$obj->where("del=0 and user_id in($user_id)")->limit("$page->firstRow,$page->listRows")->order("get_time desc")->select();
            }

        }else{
            $count=$obj->where("del=0")->count();
            $page=new Page($count,$num);
            $arr=$obj->where("del=0")->limit("$page->firstRow,$page->listRows")->order("get_time desc")->select();
        }

        //显示用户名
        foreach($arr as $userk=>$userv){
            $name=M("user_list")->field("name")->where("user_id='".$userv['user_id']."'")->find();
            $arr[$userk]['user_ids']=$name['name'];
        }

        //显示类型
        foreach($arr as $typek=>$typev){
            $type='';
            if($typev['type']==0){
                $type='支出';
            }else{
                $type='收入';
            }
            $arr[$typek]['types']=$type;
        }
        $user=array(
            '3'=>'用户'
        );
        $show=$page->show();
        $this->assign("type",$user);
        $this->assign('page',$show);
        $this->assign("data",$arr);
        //传递搜索条件
        $this->assign('type_id',$type_id);
        $this->assign('key_world',$key_world);
        $this->display();
    }

    /*
       *
       * 用户成长值列表删除
       * */
    public function growth_userdels(){
        $id=I('id');
        $data['del']=1;
        $re=M("growth_user")->where("growth_id='".$id."'")->save($data);
        if($re!=false){
            $this->redirect('growth_user','',3,'删除成功！');
        }else{
            $this->redirect('growth_user','',3,'删除失败！');
        }
    }



    /*
  *
  * 用户收藏夹列表
  * */
    public function user_favorites(){
        $type_id=I("post.type_id");
        $key_world=I("post.goods_name");

        $obj=M("user_favorites");
        $num=10;
        if($type_id==3){
            $user_id='';
            $user_ids=M("user_list")->field("user_id")->where("name like '%{$key_world}%'")->select();
            foreach($user_ids as $idk=>$idv){
                $user_id.=$idv['user_id'].',';
            }
            $user_id=trim("$user_id",",");
            if($user_id==''){
                $this->redirect('user_favorites','',3,'没有匹配的记录！');
            }else{
                $count=$obj->where("del=0 and user_id in($user_id)")->count();
                $page=new Page($count,$num);
                $arr=$obj->where("del=0 and user_id in($user_id)")->limit("$page->firstRow,$page->listRows")->order("collect_time desc")->select();
            }

        }else{
            $count=$obj->where("del=0")->count();
            $page=new Page($count,$num);
            $arr=$obj->where("del=0")->limit("$page->firstRow,$page->listRows")->order("collect_time desc")->select();
        }

        //显示用户名
        foreach($arr as $userk=>$userv){
            $name=M("user_list")->field("name")->where("user_id='".$userv['user_id']."'")->find();
            $arr[$userk]['user_ids']=$name['name'];
        }

        //显示商品名
        foreach($arr as $goodsk=>$goodsv){
            $goodsname=M("goods_list")->field('goods_name')->where("goods_id='".$goodsv['goods_id']."'")->find();
            $arr[$goodsk]['goods_ids']=$goodsname['goods_name'];
        }

        //显示类型
        foreach($arr as $typek=>$typev){
            if($typev['goods_type']==0){
                $type='商城商品';
            }else{
                $type='积分商城商品';
            }
            $arr[$typek]['goods_types']=$type;
        }
        $user=array(
            '3'=>'用户'
        );
        $show=$page->show();
        $this->assign("type",$user);
        $this->assign('page',$show);
        $this->assign("data",$arr);
        //传递搜索条件
        $this->assign('type_id',$type_id);
        $this->assign('key_world',$key_world);
        $this->display();
    }

    /*
       *
       * 用户收藏夹列表删除
       * */
    public function user_favoritesdel(){
        $id=I('id');
        $data['del']=1;
        $re=M("user_favorites")->where("favorites_id='".$id."'")->save($data);
        if($re!=false){
            $this->redirect('user_favorites','',3,'删除成功！');
        }else{
            $this->redirect('user_favorites','',3,'删除失败！');
        }
    }
    /*
 *
 * 用户登录记录列表
 * */
    public function user_login(){
        $type_id=I("post.type_id");
        $key_world=I("post.goods_name");

        $obj=M("user_login");
        $num=10;
        if($type_id==3){
            $user_id='';
            $user_ids=M("user_list")->field("user_id")->where("name like '%{$key_world}%'")->select();
            foreach($user_ids as $idk=>$idv){
                $user_id.=$idv['user_id'].',';
            }
            $user_id=trim("$user_id",",");
            if($user_id==''){
                $this->redirect('user_login','',3,'没有匹配的记录！');
            }else{
                $count=$obj->where("del=0 and user_id in($user_id)")->count();
                $page=new Page($count,$num);
                $arr=$obj->where("del=0 and user_id in($user_id)")->limit("$page->firstRow,$page->listRows")->order("login_time desc")->select();
            }

        }else{
            $count=$obj->where("del=0")->count();
            $page=new Page($count,$num);
            $arr=$obj->where("del=0")->limit("$page->firstRow,$page->listRows")->order("login_time desc")->select();
        }

        //显示用户名
        foreach($arr as $userk=>$userv){
            $name=M("user_list")->field("name")->where("user_id='".$userv['user_id']."'")->find();
            $arr[$userk]['user_ids']=$name['name'];
        }
        $user=array(
            '3'=>'用户'
        );
        $show=$page->show();
        $this->assign("type",$user);
        $this->assign('page',$show);
        $this->assign("data",$arr);
        //传递搜索条件
        $this->assign('type_id',$type_id);
        $this->assign('key_world',$key_world);
        $this->display();
    }

    /*
       *
       * 用户登录记录列表删除
       * */
    public function user_logindel(){
        $id=I('id');
        $data['del']=1;
        $re=M("user_login")->where("record_id='".$id."'")->save($data);
        if($re!=false){
            $this->redirect('user_login','',3,'删除成功！');
        }else{
            $this->redirect('user_login','',3,'删除失败！');
        }
    }

    /*
     *
     * 积分记录列表
     * */
    public function integral_user(){
        $type_id=I("post.type_id");
        $key_world=I("post.goods_name");
        $pai=I("post.pai")==''?'integral_id':I("post.pai");
        $obj=M("integral_user");
        $num=10;
        if($type_id==5){
            $arr='';
            $user_id=M("user_list")->field('user_id')->where("name like '%{$key_world}%'")->select();
            foreach($user_id as $k=>$v){
                $arr.=$v['user_id'].',';
            }
            $arr=trim($arr,',');
            if($arr==''){//排除IN 询条件为空导致的 sql 语法错误
                $this->redirect("integral_user",'',3,'没有匹配的记录！');
            }else{
                $count=$obj->where("del=0 and user_id in($arr)")->count();
                $Page=new Page($count,$num);
                $page=$Page->show();
                $data=$obj->where("del=0 and user_id in($arr)")->limit("$Page->firstRow,$Page->listRows")->order("$pai desc")->select();
            }
        }else{
            $count=$obj->where("del=0")->count();
            $Page=new Page($count,$num);
            $page=$Page->show();
            $data=$obj->where("del=0")->limit("$Page->firstRow,$Page->listRows")->order("$pai desc")->select();
        }
        //显示用户
        foreach($data as $infok=>$infov){
            $goods_name=M("user_list")->field("name")->where("user_id='".$infov['user_id']."'")->find();
            $data[$infok]['user_ids']=$goods_name['name'];
        }
        $type=array(
            '5'=>'用户'
        );
        $this->assign('type',$type);
        $this->assign("page",$page);
        $this->assign("data",$data);
        //传递搜索条件
        $this->assign('type_id',$type_id);
        $this->assign('key_world',$key_world);
        $this->assign('pai',$pai);
        $this->display();
    }

    /*
      *
      * 积分记录列表删除
      * */
    public function integral_userdel(){
        $id=I('id');
        $data['del']=1;
        $re=M("integral_user")->where("integral_id='".$id."'")->save($data);
        if($re!=false){
            $this->redirect('integral_user','',3,'删除成功！');
        }else{
            $this->redirect('integral_user','',3,'删除失败！');
        }
    }


    /*
    *
    * 用户邀请记录列表
    * */
    public function invite_record(){

        $type_id=I("post.type_id");
        $key_world=I("post.goods_name");
        $obj=M("invite_record");
        $num=10;
        if($type_id==5 || $type_id==6){
            $arr='';
            $user_id=M("user_list")->field('user_id')->where("name like '%{$key_world}%'")->select();
            foreach($user_id as $k=>$v){
                $arr.=$v['user_id'].',';
            }
            $arr=trim($arr,',');
            if($arr==''){//排除IN 询条件为空导致的 sql 语法错误
                $this->redirect("invite_record",'',3,'没有匹配的记录！');
            }else{
                $count=$obj->where("del=0 and user_id in($arr)")->count();
                $Page=new Page($count,$num);
                $page=$Page->show();
                $data=$obj->where("del=0 and user_id in($arr)")->limit("$Page->firstRow,$Page->listRows")->order("invite_time desc")->select();
            }
        }else{
            $count=$obj->where("del=0")->count();
            $Page=new Page($count,$num);
            $page=$Page->show();
            $data=$obj->where("del=0")->limit("$Page->firstRow,$Page->listRows")->order("invite_time desc")->select();
        }
        //显示用户
        foreach($data as $infok=>$infov){
            $goods_name=M("user_list")->field("name")->where("user_id='".$infov['user_id']."'")->find();
            $data[$infok]['user_ids']=$goods_name['name'];
        }

        //显示邀请人
        foreach($data as $infok=>$infov){
            $name=M("user_list")->field("name")->where("user_id='".$infov['inviter_id']."'")->find();
            $data[$infok]['inviter_ids']=$name['name'];
        }
        $type=array(
            '5'=>'用户',
            '6'=>'邀请人'
        );
        $this->assign('type',$type);
        $this->assign("page",$page);
        $this->assign("data",$data);
        //传递搜索条件
        $this->assign('type_id',$type_id);
        $this->assign('key_world',$key_world);
        $this->display();
    }

    /*
      *
      * 用户邀请记录列表删除
      * */
    public function invite_recorddel(){
        $id=I('id');
        $data['del']=1;
        $re=M("invite_record")->where("record_id='".$id."'")->save($data);
        if($re!=false){
            $this->redirect('invite_record','',3,'删除成功！');
        }else{
            $this->redirect('invite_record','',3,'删除失败！');
        }
    }

    /*
   *
   * 用户操作记录列表
   * */
    public function user_operation(){

        $type_id=I("post.type_id");
        $key_world=I("post.goods_name");
        $obj=M("user_operation");
        $num=10;
        if($type_id==5){
            $arr='';
            $user_id=M("user_list")->field('user_id')->where("name like '%{$key_world}%'")->select();
            foreach($user_id as $k=>$v){
                $arr.=$v['user_id'].',';
            }
            $arr=trim($arr,',');
            if($arr==''){//排除IN 询条件为空导致的 sql 语法错误
                $this->redirect("user_operation",'',3,'没有匹配的记录！');
            }else{
                $count=$obj->where("del=0 and user_id in($arr)")->count();
                $Page=new Page($count,$num);
                $page=$Page->show();
                $data=$obj->where("del=0 and user_id in($arr)")->limit("$Page->firstRow,$Page->listRows")->order("operation_time desc")->select();
            }
        }else{
            $count=$obj->where("del=0")->count();
            $Page=new Page($count,$num);
            $page=$Page->show();
            $data=$obj->where("del=0")->limit("$Page->firstRow,$Page->listRows")->order("operation_time desc")->select();
        }
        //显示用户
        foreach($data as $infok=>$infov){
            $goods_name=M("user_list")->field("name")->where("user_id='".$infov['user_id']."'")->find();
            $data[$infok]['user_ids']=$goods_name['name'];
        }

        $type=array(
            '5'=>'用户'
        );
        $this->assign('type',$type);
        $this->assign("page",$page);
        $this->assign("data",$data);
        //传递搜索条件
        $this->assign('type_id',$type_id);
        $this->assign('key_world',$key_world);
        $this->display();
    }

    /*
      *
      * 用户邀请记录列表删除
      * */
    public function user_operationdel(){
        $id=I('id');
        $data['del']=1;
        $re=M("user_operation")->where("record_id='".$id."'")->save($data);
        if($re!=false){
            $this->redirect('user_operation','',3,'删除成功！');
        }else{
            $this->redirect('user_operation','',3,'删除失败！');
        }
    }

}

